from typing import Any, Dict, Optional, Union
from enum import Enum
import time


class ResponseCode(Enum):
    """响应状态码枚举类"""
    SUCCESS = 200  # 成功
    PARAM_ERROR = 400  # 参数错误
    UNAUTHORIZED = 401  # 未授权
    FORBIDDEN = 403  # 禁止访问
    NOT_FOUND = 404  # 资源不存在
    SERVER_ERROR = 500  # 服务器错误
    SERVICE_UNAVAILABLE = 503  # 服务不可用


class ResponseResult:
    """统一响应结果类"""
    
    @staticmethod
    def success(data: Any = None, message: str = "操作成功") -> Dict[str, Any]:
        """
        成功响应
        
        Args:
            data: 响应数据
            message: 响应消息
            
        Returns:
            Dict: 统一格式的响应字典
        """
        return {
            "code": ResponseCode.SUCCESS.value,
            "message": message,
            "data": data,
            "timestamp": int(time.time() * 1000)
        }
    
    @staticmethod
    def error(code: Union[int, ResponseCode], message: str, data: Any = None) -> Dict[str, Any]:
        """
        错误响应
        
        Args:
            code: 错误码
            message: 错误消息
            data: 额外数据
            
        Returns:
            Dict: 统一格式的响应字典
        """
        if isinstance(code, ResponseCode):
            code = code.value
            
        return {
            "code": code,
            "message": message,
            "data": data,
            "timestamp": int(time.time() * 1000)
        }
    
    @staticmethod
    def param_error(message: str = "参数错误", data: Any = None) -> Dict[str, Any]:
        """参数错误响应"""
        return ResponseResult.error(ResponseCode.PARAM_ERROR, message, data)
    
    @staticmethod
    def unauthorized(message: str = "未授权", data: Any = None) -> Dict[str, Any]:
        """未授权响应"""
        return ResponseResult.error(ResponseCode.UNAUTHORIZED, message, data)
    
    @staticmethod
    def forbidden(message: str = "禁止访问", data: Any = None) -> Dict[str, Any]:
        """禁止访问响应"""
        return ResponseResult.error(ResponseCode.FORBIDDEN, message, data)
    
    @staticmethod
    def not_found(message: str = "资源不存在", data: Any = None) -> Dict[str, Any]:
        """资源不存在响应"""
        return ResponseResult.error(ResponseCode.NOT_FOUND, message, data)
    
    @staticmethod
    def server_error(message: str = "服务器错误", data: Any = None) -> Dict[str, Any]:
        """服务器错误响应"""
        return ResponseResult.error(ResponseCode.SERVER_ERROR, message, data)
    
    @staticmethod
    def service_unavailable(message: str = "服务不可用", data: Any = None) -> Dict[str, Any]:
        """服务不可用响应"""
        return ResponseResult.error(ResponseCode.SERVICE_UNAVAILABLE, message, data) 